package com.sgb.member.view_model;


import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberFindPackersEntity;
import com.sgb.member.ui.fragment.BusinessTeamFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class BusinessTeamModel extends BaseViewModel {

    private BusinessTeamFragment fragment;
    private MemberRepertory repertory;
    private final int maxPageSize = 10;
    private int current;
    private CommonPopEntity mAll;
    public BusinessTeamModel(BusinessTeamFragment activity, MemberRepertory repertory) {
        this.fragment = activity;
        this.repertory = repertory;
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }

    public void teamList(boolean isLoadMore, String companyName, String teamType,
                         String mNo1, String mNo2, String mNo3,String mNo4,
                         String mNo6, String mNo7, String level) {
        int currentPage = isLoadMore ? current + 1 : 1;
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("page", currentPage);
        map.put("limit", maxPageSize);
        map.put("level", level);
        map.put("teamType", teamType);//团队类型 0施工队 1班组
        map.put("sortType", "1");//1按照时间 2按照价格
        map.put("firstCategoryNo", mNo1);
        map.put("secondCategoryNo", mNo2);
        map.put("thirdCategoryNo", mNo3);
        map.put("teamSize", mNo4); //团队规模
        map.put("provinceNo", mNo6); //所在地区-省
        map.put("cityNo", mNo7); ////所在地区-市
        repertory.getApi().teamList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberFindPackersEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberFindPackersEntity>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberFindPackersEntity>>>> call, Throwable t) {
                super.onFailure(call, t);
                if(!isLoadMore){
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFinally() {
                showOrHideDialog(false);
            }
        });
    }

    // 工队-工程分类 type(1 一级分类  type=0 三级分类  type =2 二级分类 type=null  六级分类)
    public void getCategoryTrees(int type, boolean isShowLeftDialog, boolean isShowPopup) {
        showOrHideDialog(true);
        repertory.getApi().getCategoryTrees(repertory.getAreaCode(), "0").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData(), isShowPopup);
                    }
                }
            }
        });
    }

    // 工队团队规模
    public void getTeamScale(int type, boolean isCompany, boolean isShowLeftDialog, boolean isShowPopup) {
        showOrHideDialog(true);
        // 团队类型 ： 施工队 :0 班组  :1
        repertory.getApi().getTeamScale(repertory.getAreaCode(), isCompany? "0" : "1").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData(), isShowPopup);
                    }
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(int type, boolean isShowPopup) {
        showOrHideDialog(true);
        repertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                showOrHideDialog(false);
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    fragment.setAddressData(data.getData());
                    fragment.setChooseItemData(type, entities,isShowPopup);
                }
            }
        });
    }

    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no, int type, boolean isShowLeftDialog) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new CommonPopEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, entities);
                    } else {
                        fragment.setChooseItemData(type, entities, false);
                    }
                }
            }
        }
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getUrlSetting("teamWorkDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?no=" + id + "&type=0";
                MLog.d("api", "H5跳转链接  " + url);
                MemberConfig.getInstance().startWebViewX5(fragment.getActivity(), url);
            }
        });
    }

    public void getMemberLevelData(int type, boolean isShowLeftDialog) {
        repertory.getApi().getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData(), false);
                    }
                }
            }
        });
    }



}
